package com.inossem.warehousestorage_inossem.adapter.query;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.query.QueryBatchMaterielListActivity;
import com.inossem.warehousestorage_inossem.activity.query.QueryMaterielDetailActivity;
import com.inossem.warehousestorage_inossem.activity.query.extra.QueryExtra;
import com.inossem.warehousestorage_inossem.bean.query.QueryMaterielBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class QueryMaterielListAdapter extends BaseAdapter {

    private static final String SPECIAL_INVENTORY_CODE = "特殊库存编码:";

    private static final String POSITION = "仓位:";

    private static final String BATCH = "批次:";

    private Context mContext;
    private List<QueryMaterielBean.DataBean.MaterielListBean> mMaterielList;
    private LayoutInflater inflater;
    private QueryBatchMaterielListActivity.BatchMaterielListener mBatchMaterielListener;
    private String mCondition;

    public QueryMaterielListAdapter(Context context, List<QueryMaterielBean.DataBean.MaterielListBean> materielList, String condition, QueryBatchMaterielListActivity.BatchMaterielListener batchMaterielListener) {
        mContext = context;
        mMaterielList = materielList;
        mBatchMaterielListener = batchMaterielListener;
        inflater = LayoutInflater.from(context);
        mCondition = condition;
    }

    @Override
    public int getCount() {
        return mMaterielList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mMaterielList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_receive_list_first_item, null);
            EditText condition = (EditText) convertView.findViewById(R.id.inStorageNumber);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            condition.setHint(mContext.getResources().getString(R.string.activity_change_storage_materiel_list_please_input_material_information));
//            condition.setText(mCondition);
            condition.addTextChangedListener(new QRCodeEditTextWatcher(condition) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, condition);
                    mBatchMaterielListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mBatchMaterielListener.onNoDoubleClickListener(condition.getText().toString());
                }
            });
        } else {
            QueryMaterielBean.DataBean.MaterielListBean materielItem = mMaterielList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_change_storage_materiel_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, materielItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.specialInventoryFlag = (TextView) view.findViewById(R.id.specialInventoryCode);
        viewHolder.materielDescribe = (TextView) view.findViewById(R.id.materielDescribe);
        viewHolder.position = (TextView) view.findViewById(R.id.position);
        viewHolder.batch = (TextView) view.findViewById(R.id.batch);
        viewHolder.number = (TextView) view.findViewById(R.id.number);
        viewHolder.headBar = (PercentRelativeLayout) view.findViewById(R.id.headBar);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, QueryMaterielBean.DataBean.MaterielListBean materielItem) {
        viewHolder.materielCode.setText(materielItem.getMaterielCode());
        if (TextUtils.isEmpty(materielItem.getSpecialInventoryFlag())) {
            viewHolder.specialInventoryFlag.setVisibility(View.GONE);
        } else {
            viewHolder.specialInventoryFlag.setVisibility(View.VISIBLE);
            viewHolder.specialInventoryFlag.setText(getBuilder(context, SPECIAL_INVENTORY_CODE, materielItem.getSpecialInventoryFlag()));
        }
        viewHolder.position.setText(getBuilder(context, POSITION, materielItem.getPositionNumber()));
        viewHolder.batch.setText(getBuilder(context, BATCH, materielItem.getBatchNumber()));
        viewHolder.materielDescribe.setText(materielItem.getMaterielDescription());
        viewHolder.number.setTextColor(ContextCompat.getColor(context, R.color.common_font_content));
        viewHolder.number.setText(materielItem.getInventoryNumber() + materielItem.getUnit());
        viewHolder.headBar.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, QueryMaterielDetailActivity.class);
                intent.putExtra(QueryExtra.ID, materielItem.getId());
                intent.putExtra(QueryExtra.TYPE, QueryMaterielDetailActivity.BATCH);
                context.startActivity(intent);
            }
        });
    }

    private SpannableStringBuilder getBuilder(Context context, String changeColorMessage, String message) {
        SpannableStringBuilder builder = new SpannableStringBuilder(changeColorMessage + message);
        ForegroundColorSpan redSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.common_font_item_name));
        builder.setSpan(redSpan, 0, changeColorMessage.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return builder;
    }

    private class ViewHolder {
        private TextView materielCode, specialInventoryFlag, materielDescribe, position, batch, number;
        private PercentRelativeLayout headBar;
    }
}
